package cn.zhou.student.controller;


import cn.zhou.student.bean.PageBean;
import cn.zhou.student.bean.Student;
import cn.zhou.student.bean.pageCondition;
import cn.zhou.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class StudentController {
    @Autowired
    private StudentService service;

    @GetMapping("/student/getConditionPageStudent")
    public String getConditionPageStudent(pageCondition pageCondition, HttpServletRequest request){
        String currentPage=pageCondition.getCurrentPage()+"";
        PageBean pageStudent = service.getConditionPageStudent(pageCondition,currentPage);
        request.setAttribute("students",pageStudent);
        request.setAttribute("condition",pageCondition);
        return "user/userList";
    }
    @GetMapping("/add/addPage")
    public String addPage(){
        return "user/userAdd";
    }
    @PostMapping("/add/addStudent")
    public String addStudent(Student student){//传入的参数并不是json所有不需要用requestBody
        service.addStudent(student);
        return "redirect:/student/getConditionPageStudent";
    }
    @GetMapping("delete/deleteStudent")
    public String deleteStudent(String id){
        service.delete(id);
        return "redirect:/student/getConditionPageStudent";
    }

    //信息回显,根据id查学生
    @GetMapping("/update/getById")
    public String getById(String id, HttpServletRequest request){
        Student student=service.getById(id);
        request.setAttribute("student",student);
        return "user/userEdit";
    }
    @PostMapping("/update/updateStudent")
    public String updateStudent(Student student){
        service.update(student);
        return "redirect:/student/getConditionPageStudent";
    }
    @GetMapping("user/index")
    public String niubi(){
        return "user/userIndex";
    }

}
